package com.supermap.gwfs.datashare.of.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.supermap.gwfs.datashare.entity.GridData;
import com.supermap.gwfs.datashare.entity.GridForecast;
import com.supermap.gwfs.datashare.entity.StationInfo;
import org.springframework.stereotype.Service;

@Service
public class FinalResultService {
	/**
	 * 
	 * @param getStationInfos
	 * @param gridForecasts
	 * @param rootPath
	 * @param object
	 * @param lonStaion
	 * @param latStaion
	 * @return
	 */
	public float query(List<StationInfo> getStationInfos,List<Object> gridForecasts,String rootPath,Object object,Float lonStaion,Float latStaion){
		NetcdfService netcdfService = new NetcdfService();
		GridForecast gf = (GridForecast)object;
		String element = gf.getElement_value();
		String filePath = rootPath + "/" + gf.getForecast_filepath();
		//读取netcdf文件数据
		GridData gridData = netcdfService.getGridDataFromNetcdf(filePath , element);
		//纬度分辨率
		float latResolving=gridData.getLatResolution();
		//经度分辨率
		float lonResolving=gridData.getLonResolution();
		BigDecimal lonji = new BigDecimal((lonStaion-lonResolving)/lonResolving).setScale(2, RoundingMode.UP);
		BigDecimal latji = new BigDecimal((latStaion-latResolving)/latResolving).setScale(2, RoundingMode.UP);
		//文件的经纬度范围
		float startLat=gridData.getStartLat();
		float endLat=gridData.getEndLat();
		float startLon=gridData.getStartLon();
		float endLon=gridData.getEndLon();
		int lat = this.getLat(startLat, endLat, latResolving, lonResolving, lonji, latji).intValue();
		int lon = this.getLon(startLon, endLon, latResolving, lonResolving, lonji, latji).intValue();
		float[][] reuslt=gridData.getValues();
		float finalResult=reuslt[lat][lon];
		return finalResult;
		
	}
		
	/**
	 * 
	 * @param startLat
	 * @param endLat
	 * @param latResolving
	 * @param lonResolving
	 * @param lonji
	 * @param latji
	 * @return
	 */
		public BigDecimal  getLat(float startLat,float endLat,float latResolving,float lonResolving,BigDecimal lonji,BigDecimal latji) {
			float lat=0f;
			float lon=0f;
			BigDecimal ybg=null;
			for(float j = 0 ; j<=endLat;j=j+latResolving){
					//计算纬度是否和站点匹配
					lat=startLat+latResolving;
					float y=((startLat+latResolving)-startLat)/latResolving;
					ybg = new BigDecimal(y).setScale(0, RoundingMode.UP);
					if (lonji==ybg) {
						
						break;
					}
			}
			return ybg;
		}	
		/**
		 * 
		 * @param startLon
		 * @param endLon
		 * @param latResolving
		 * @param lonResolving
		 * @param lonji
		 * @param latji
		 * @return
		 */
		public BigDecimal  getLon(float startLon,float endLon,float latResolving,float lonResolving,BigDecimal lonji,BigDecimal latji) {
			float lat=0f;
			float lon=0f;
			BigDecimal xbg=null;
				for(float j = 0 ; j<=endLon;j=j+lonResolving){
						lon=startLon+lonResolving;
						//计算经度是否和站点匹配
						float x=((startLon+lonResolving)-startLon)/lonResolving;
						xbg = new BigDecimal(x).setScale(0, RoundingMode.UP);
						if (lonji==xbg) {
							
							break;
						}
				}
				return xbg;
		}
		
	/**
	 * 气象规则判断开始方法
	 * @param finalResult 降水
	 * @param element 要素
	 * @param Tcc	总云量
	 * @param Tcc1	总云量判断参数1
	 * @param Tcc2	总云量判断参数2
	 * @param VIS	能见度
	 * @param V0	能见度判断参数
	 * @param RH	相对湿度
	 * @param Rh0	相对湿度判断值
	 * @param Ts	地表温度
	 * @param T0	地表温度判断参数0
	 * @param T1	地表温度判断参数1
	 * @param R1	小雨
	 * @param R2	中雨
	 * @param R3	大雨
	 * @param R4	暴雨及大暴雨
	 * @param R11	小雪
	 * @param R22	中雪
	 * @param R33	大雪
	 * @param R44	暴雪
	 * @return
	 */
	public String getValueJudgeMethod(float light,int ppH,float rain,float Tcc,float Tcc1,float Tcc2,float VIS,float V0,float RH,float Rh0,float Ts,float T0,float T1,float R1,float R2,float R3,float R4,float R11,float R22,float R33,float R44){
		JSONObject getValueJudgeMethod = this.isRain(light,ppH,rain, Tcc, Tcc1, Tcc2, VIS, V0, RH, Rh0, Ts, T0, T1, R1, R2, R3, R4, R11, R22, R33, R44);
		String result=getValueJudgeMethod.toString();
		return result;
	}
	/**
	 * 气象规则判断开始方法
	 * @param finalResult 降水
	 * @param element 要素
	 * @param Tcc	总云量
	 * @param Tcc1	总云量判断参数1
	 * @param Tcc2	总云量判断参数2
	 * @param VIS	能见度
	 * @param V0	能见度判断参数
	 * @param RH	相对湿度
	 * @param Rh0	相对湿度判断值
	 * @param Ts	地表温度
	 * @param T0	地表温度判断参数0
	 * @param T1	地表温度判断参数1
	 * @param R1	小雨
	 * @param R2	中雨
	 * @param R3	大雨
	 * @param R4	暴雨及大暴雨
	 * @param R11	小雪
	 * @param R22	中雪
	 * @param R33	大雪
	 * @param R44	暴雪
	 * @return
	 */
	public JSONObject isRain(float light,int ppH,float rain,float Tcc,float Tcc1,float Tcc2,float VIS,float V0,float RH,float Rh0,float Ts,float T0,float T1,float R1,float R2,float R3,float R4,float R11,float R22,float R33,float R44){
		JSONObject result = new JSONObject();
		JSONObject result1 = new JSONObject();
		JSONObject result2 = new JSONObject();
		JSONObject result3 = new JSONObject();
		if (rain > 0) {
			result1 = this.isVIS(rain, V0);
//			result2 = this.isTs(Ts, finalResult, T0, T1, R1, R2, R3, R4, R11, R22, R33, R44);
			result2 = this.isPPH(ppH, rain, R1, R2, R3, R4, R11, R22, R33, R44);
			result3 = this.isLight(light, rain, R11);
			result.put("VIS", result1);
//			result.put("Ts", result2);
			result.put("PPH", result2);
			result.put("Light", result3);
			
		}else{
			result1 = this.isTcc(Tcc,  Tcc1, Tcc2);
			if (RH < Rh0) {
				result2 = this.isRH(RH, Rh0);
			}
			result.put("Tcc", result1);
			result.put("RH", result2);
		}
		
		return result;	
	}
	
	/**
	 * 判断雾霾
	 * @param RH
	 * @param Rh0
	 * @param element
	 * @return
	 */
	public JSONObject isRH(float RH,float Rh0){
		JSONObject result = new JSONObject();
		if (RH > Rh0) {
			String judge = "雾";
			result.put("value", RH);
			result.put("judge", judge);
		}else {
			String judge = "霾";
			result.put("value", RH);
			result.put("judge", judge);
		}
		return result;
	}
	/**
	 * 判断总云量
	 * @param Tcc
	 * @param element
	 * @param Tcc1
	 * @param Tcc2
	 * @return
	 */
	public JSONObject isTcc(float Tcc,float Tcc1,float Tcc2){
		JSONObject result = new JSONObject();
		if (0 <= Tcc && Tcc < Tcc1 ) {
			String judge = "晴";
			result.put("value", Tcc);
			result.put("judge", judge);
		}else if(Tcc1 <= Tcc && Tcc < Tcc2){
			String judge = "多云";
			result.put("value", Tcc);
			result.put("judge", judge);
		}
		else if(Tcc2 <= Tcc && Tcc <= 100){
			String judge = "阴";
			result.put("value", Tcc);
			result.put("judge", judge);
		}
		return result;
	}
	

	/**
	 * 云状部分判断
	 * @param gridDataETC 逐3小时该店的值
	 * @param etcXMLTcc1	<=20%
	 * @param etcXMLTcc2	<=90%
	 */
	public JSONObject isETC(float gridDataETC,float etcXMLTcc1,float etcXMLTcc2){
		JSONObject result = new JSONObject();
		if (gridDataETC <= etcXMLTcc1) {
			String judge = "晴";
			result.put("judge", judge);
			result.put("value", gridDataETC);
		}else if (gridDataETC <= etcXMLTcc2) {
			String judge = "阴";
			result.put("judge", judge);
			result.put("value", gridDataETC);
		}else {
			String judge = "多云";
			result.put("judge", judge);
			result.put("value", gridDataETC);
		}
		return result;
	}
	
	
	/**
	 * 判断VIS能见度
	 * @param VIS 传过来的能见度值
	 * @param V0 判断参数
	 * @param element 要素
	 * @return
	 */
	public JSONObject isVIS(float VIS,float V0){
		JSONObject result = new JSONObject();
		if (VIS < V0) {
//			StringBuffer result = new StringBuffer();
			String judge = "雨雾";
			result.put("value", VIS);
			result.put("judge", judge);
		}
		return result;
	}
	/**
	 * * 地表温度判读
	 * @param Ts 传过来的地表温度值
	 * @param finalResult 传过来的值
	 * @param T0 判断参数
	 * @param element	要素
	 * @param R1	小雨
	 * @param R2	中雨
	 * @param R3	大雨
	 * @param R4	暴雨or大暴雨
	 * @param R11	小雪
	 * @param R22	中雪
	 * @param R33	大雪
	 * @param R44	暴雪
	 * @return
	 */
	public JSONObject isTs(float Ts,float finalResult,float rain,float T1,float R1,float R2,float R3,float R4,float R11,float R22,float R33,float R44){
		JSONObject result = null;
		if (Ts > rain) {
			result = this.isT0(rain,  R1, R2, R3, R4);	//判断降水
		}else if (T1 <= Ts && Ts < rain) {		
		}else if (T1 > Ts) {
			result = this.isT1(rain,  R11, R22, R33, R44);//判断降雪
		}
		return result;
	}
	/**
	 * 降水判断
	 * @param finalResult
	 * @param T0
	 * @param element
	 * @param R1
	 * @param R2
	 * @param R3
	 * @param R4
	 * @return
	 */
	public JSONObject isT0(float rain,float R1,float R2,float R3,float R4){
		JSONObject result = new JSONObject();
		if (rain < R1) {
			String judge = "小雨 阵雨 间断小雨";
			result.put("value", rain);
			result.put("judge", judge);
		}else if (R1 <= rain && rain < R2) {
			String judge = "中雨";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(R2 <= rain && rain < R3){
			String judge = "大雨";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(R3 <= rain && rain < R4){
			String judge = "暴雨";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(rain <= R4){
			String judge = "大暴雨";
			result.put("value", rain);
			result.put("judge", judge);
		}
		return result;
	}
	/**
	 * 降雪判断
	 * @param finalResult
	 * @param T0
	 * @param T1
	 * @param element
	 * @param R11
	 * @param R22
	 * @param R33
	 * @param R44
	 * @return
	 */
	public JSONObject isT1(float rain,float R11,float R22,float R33,float R44){
		JSONObject result = new JSONObject();
		if(rain < R11){
			String judge = "小雪 阵雪 间断小雪";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(R11 <= rain && rain < R22){
			String judge = "中雪";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(R22 <= rain && rain < R33){
			String judge = "大雪";
			result.put("value", rain);
			result.put("judge", judge);
		}else if(R33 <= rain && rain < R44){
			String judge = "暴雪";
			result.put("value", rain);
			result.put("judge", judge);
		}
		return result;
	}
	/**
	 * 判断雨夹雪
	 * @param rain 
	 * @param R1
	 * @param R2
	 * @param R3
	 * @param R4
	 * @param element
	 * @param T0
	 * @return
	 */
	public JSONObject isRainShonw(float rain,float R1,float R2,float R3,float R4){
		JSONObject result = new JSONObject();
		if (rain < R1) {
			String judge = "雨夹雪";
			result.put("value", rain);
			result.put("judge", judge);
		}else{
			result = this.isT0(rain,  R1, R2, R3, R4);	//判断降水
		}
		return result;
	}
	/**
	 * 相态文件判断
	 * @param ppH
	 * @param rain
	 * @param R1
	 * @param R2
	 * @param R3
	 * @param R4
	 * @param R11
	 * @param R22
	 * @param R33
	 * @param R44
	 * @return
	 */
	public JSONObject isPPH(int ppH,float rain,float R1,float R2, float R3,float R4,float R11,float R22, float R33,float R44){
		JSONObject result = new JSONObject();
		if(ppH==1){
			result = this.isT0(rain, R1, R2, R3, R4);
		}else if(ppH==2) {
			result = this.isRainShonw(rain, R1, R2, R3, R4);
		}else if (ppH == 3) {
			result = this.isT1(rain, R11, R22, R33, R44);
		}else if (ppH == 4) {
			result.put("judge", "冻雨");
		}
		return result;
	}
	/**
	 * 雷电判断
	 * @param light
	 * @param rain
	 * @param R1
	 * @return
	 */
	public JSONObject isLight(float light,float rain,float R1){
		JSONObject result = new JSONObject();
		
		if (true) {
			if (rain < R1) {
				String judge = "雷阵雨";
				result.put("value", rain);
				result.put("judge", judge);
			}
		}
		return result;
		
	}

}
